package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.Question;
import com.example.computer_exam_training_sys.mapper.QuestionMapper;
import com.example.computer_exam_training_sys.service.QuestionService;
import com.example.computer_exam_training_sys.entity.adminlogsInfo;
import com.example.computer_exam_training_sys.mapper.admin_logsMapper;
import com.example.computer_exam_training_sys.service.admin_logsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public PageInfo<Question> findAllpage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Question> questions = questionMapper.selectAll();
        return new PageInfo<Question>(questions);
    }

    @Autowired
    private admin_logsMapper adminLogsMapper;

    @Override
    public List<Question> selectBypointId(Integer pointId) {
        return questionMapper.selectBypointId(pointId);
    }

    @Override
    public int insert(Question question) {
        return questionMapper.insert(question);
    }

    @Override
    public Question selectById(Integer questionId) {
        return questionMapper.selectById(questionId);
    }

    @Override
    public List<Question> selectAll() {
        return questionMapper.selectAll();
    }

    @Override
    public int update(Question question) {
        int result = questionMapper.update(question);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(1); // 假设当前管理员ID为1，实际应从上下文中获取
            log.setOperationType("修改题目");
            log.setTargetId(question.getQuestionId());
            log.setTargetType("题目");
            log.setOperationDetail("修改了题目ID为 " + question.getQuestionId() + " 的内容");
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.now());
            adminLogsMapper.addLog(log);
        }
        return result;
    }

    @Override
    public int delete(Integer questionId) {
        int result = questionMapper.delete(questionId);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(1); // 假设当前管理员ID为1，实际应从上下文中获取
            log.setOperationType("删除题目");
            log.setTargetId(questionId);
            log.setTargetType("题目");
            log.setOperationDetail("删除了题目ID为 " + questionId + " 的内容");
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.now());
            adminLogsMapper.addLog(log);
        }
        return result;
    }
    @Override
    public Integer getFirstQuestionIdByPointId(Integer pointId) {
        return questionMapper.getFirstQuestionIdByPointId(pointId);
    }
}